from difflib import Differ

import wikipedia
from wiktionary_parser.xml_parser import XMLPageParser
from wiktionary_parser.languages.de.page import dePage

site = wikipedia.getSite()

xml_file = open('/Users/Ben/Web/wiktionary_parser/data/dewiktionary-20090913-pages-articles.xml')

xml_parser = XMLPageParser(xml_file, dePage)

for page in xml_parser:
    page.parse()
    if page.fixable():
        wikipage = wikipedia.Page(site, page.title)
        text = wikipage.get()
        current_page = dePage(title=page.title, text=text)
        current_page.parse()
        if current_page.fixable():
            print '-----------------------------------------'
            current_page.fix()
            new_text = current_page.render()
            if new_text != current_page.text:
                print '%s FIXABLE'  % current_page.title
                result = list(Differ().compare(current_page.text.split('\n'), new_text.split('\n')))
                for line in result:
                    if line[0] != ' ':
                        print line
                answer = None
                while answer is None:
                    answer = raw_input("Do you want to apply this change to Wiktionary? (y/n):")
                    if answer == 'y':
                        answer = True
                    elif answer == 'n':
                        answer = False
                    else:
                        answer = None
                if answer:
                    print 'making change'
                        
                    
            
            

        

    
